<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

define("EMPTY", " - ");
class Katalog extends CI_Controller {

	//Doctrine EntityManager
	public $em;

	function __construct() {
		parent::__construct();

		//Not required if you autoload the library
		$this -> load -> library('doctrine');

		//Instantiate a Doctrine Entity Manager
		$this -> em = $this -> doctrine -> em;
		// $conn = $this->em->getConnection();
		//var_dump(models\service\CurrentUser::user());
		if (models\service\CurrentUser::user() == FALSE){
			header('location: ' . base_url());
		}

		// $conn->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'varchar');
	}
	
	public function test(){
		$this->load->view('test');
	}

	// START Katalog
	public function index() {
		$data['breadcrumb'] = array( array("label" => "Home", "link" => base_url() . "dashboard.php"),
								 array("label" => "Katalog", "link" => NULL)
								);
								
		$serv = new models\service\KatalogService;
					   
		// default search bar state
		$searchBar['fieldnames'] = array( array('label' => 'Code',
												'value' => 'Barang.id'
												),
										  array('label' => 'Name',
												'value' => 'Barang.name'
												)		
										);								
		$pFieldName[0] = "Barang.id";
		$pFieldValue[0] = "";
		$pCondition[0] = "LIKE";
		$searchBy = FALSE;
		// end default search bar state
										
		if ($this->input->post('searchPar') != NULL){			
			$pFieldName=$this->input->post('fieldName');
			$pFieldValue=$this->input->post('fieldValue');
			$pCondition=$this->input->post('condition');
			
			if ($pFieldValue[0] != NULL){
				$searchBy = TRUE;
			}
		}
		
		$searchBar['pFieldName'] = $pFieldName;
		$searchBar['pCondition'] = $pCondition;
		$searchBar['pFieldValue'] = $pFieldValue;
		$data['searchBar'] = $searchBar;
		
		// default paging bar state
		$indexAndCount[0] = 1;
		$indexAndCount[1] = 5;
		$orders = array(
						array('fieldName' => 'Barang.createdDate', 'orderType' => 'DESC')
					   );
		   
		if ($this->input->post('activePage') != NULL){
			$indexAndCount[0] = $this->input->post('activePage');
			// echo "<pre>"; print_r($_POST);die();	
		}
		
		if ($this->input->post('activePageBar') != NULL){
			$indexAndCount[0] = $this->input->post('activePageBar');
		} 
		// end default paging bar state

		if ($searchBy === TRUE){
			$data['pBarang'] = $serv->paginateBarangByMap($searchBar, $indexAndCount, $orders);
		} else {
			$data['pBarang'] = $serv->paginateBarang($indexAndCount, $orders);	
		}		
		
		$this -> load -> view('katalog/index', $data);		
	}
	
	public function createEdit($id="0"){
		$data['breadcrumb'] = array( array("label" => "Home", "link" => base_url() . "dashboard.php"),
								     array("label" => "Katalog", "link" => base_url() . "katalog"),
									 array("label" => "Add New Barang", "link" => NULL)
								);
		$data['id'] = $id;
		
		if ($id != "0"){
			// init the data
			$serv = new models\service\KatalogService;
			$barang = $serv->getBarangById($id);
			$barangStock = $this -> em -> getRepository('\models\entities\BarangStock') -> findBy(array('barang' => $barang));
			$barangPrice = $this -> em -> getRepository('\models\entities\BarangPrice') -> findBy (array('barang' => $barang), array('id' => 'DESC'));
			$data['barangStock'] = $barangStock[0];
			$data['barangPrice'] = $barangPrice[0];									
		} else {
			$barang=NULL;
		}
		
		$cserv = new models\service\CategoryService;
		$data['categories'] = $cserv->findAllCategory(array('name' => 'asc'));
		
		$data['barang'] = $barang;
		$this->load->view('katalog/createEdit', $data);
	}
	
	public function saveBarang(){
		// echo "<pre>"; print_r($_POST);
		if ($this -> input -> post('Barang_id') != NULL) {
			$objects = array('Barang','BarangStock', 'BarangPrice');
			$except['Barang'] = array('createdDate');
			$except['BarangStock'] = array('createdDate', 'barang','userInput');
			$except['BarangPrice'] = array('createdDate', 'barang','userInput');
			$fields = collectingHtmlInput($objects, $except);
			// echo "<pre>"; print_r($fields);die("SAVE");
			$serv = new models\service\KatalogService;
			if ($serv->saveBarang($fields) !== FALSE){
				if ($this->input->post('ismore') == "1"){					
					//die($this->input->post('ismore')."redirect");
					header('location: ' . base_url() . 'katalog/createEdit');
				}
				
				header('location: ' . base_url() . 'katalog');	
			}			
		}	
	}
	
}

